package net.bingosoft.common.struts;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import net.bingosoft.common.BeanHelper;
import net.bingosoft.common.util.LimitUtil;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.WrapDynaBean;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.struts.validator.LazyValidatorForm;
import org.ecside.table.limit.Limit;

/**
 * 2007-9-10 上午08:58:51
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:不需要配置属性的动态Form,根据form名称进行验证
 * ChangeLog:
 */
public class BaseLazyDynaForm extends LazyValidatorForm{
	private static final long serialVersionUID = 1L;
	private int pageSize= 0;
	private Limit limit;
	private String orderBy = "";
	
	public BaseLazyDynaForm(){
		super();
		setPathValidation(false);
	}
	
	
	/**
	 * 将BO中的数据放置到Form中去
	 * @param innerBean
	 */
	public void boToForm(Object innerBean){
		if(innerBean == null) throw new IllegalArgumentException("innerBean can not be null ...");
		if (innerBean instanceof DynaBean) {
			dynaBean = (DynaBean)innerBean;
		} else {
			dynaBean = new WrapDynaBean(innerBean);
		}
	}
	/**
	 * 将form对象的数据注入到传入的boClass类型的对象中去
	 * @param <T>
	 * @param boClass
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T formToBo(Class<T> boClass){
		if(boClass == Map.class){
			return (T)getMap();
		}else{
			T target = null;
			try {
				target = boClass.newInstance();
				BeanHelper.Map2Object(getMap(), target);
			} catch (Exception e) {
				throw new RuntimeException(e);
			} 
			return target;
		}
	}
	/**
	 * 将form对象的属性注入的传入的bo对象中去
	 * @param bo
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public Object formToBo(Object bo) throws IllegalAccessException, InvocationTargetException{
		BeanHelper.Map2Object(getMap(), bo);
		return bo;
	}
	/**
	 * 判断给定form表单中的数据是否存在
	 * @param propertyName
	 * @return
	 */
	public boolean notNull(String propertyName){
		return get(propertyName) != null;
	}
	/**
	 * 获取Limit对象用于分页
	 * @return
	 */
	public Limit getLimit() {
		return limit;
	}
	/**
	 * 设置Limit
	 * @param limit
	 */
	public final void setLimit(Limit limit) {
		this.limit = limit;
		this.orderBy = LimitUtil.getOrderBy(limit);
		if(!"".equals(this.orderBy)){
			this.set("orderBy", this.orderBy);
			this.set("orderProperty", this.orderBy.replaceFirst("order by", ""));
		}
	}


	/**
	 * 获取分页大小
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * 设置分页大小
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		this.set("pageSize", this.pageSize);
	}

	/**
	 * 
	 */
	@Override
	public String toString(){
		String result = null;
		try{
			result = ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
		}catch(Exception ex){
			result = super.toString();
		}
		return result;
	}

	/**
	 * 获取参数Map(<p>Return the <code>Map</code> containing the property values.</p>)
	 * @return
	 */
	public Map getParams(){
		return super.getMap();
	}

	/**
	 * 从form中根据属性名称获取一个Integer
	 * @param src
	 * @param defaultValue
	 * @return
	 */
	public Integer getInt(String propertyName,Integer defaultValue){
		return getTargetType(propertyName, Integer.class, defaultValue);
	}
	/**
	 * 根据传入的propertyName 获取一个String类型的数据
	 * @param propertyName 属性名称
	 * @param defaultValue 当obj为null时返回的默认字符串
	 * @return
	 */
	public String getString(String propertyName,String defaultValue){
		return getTargetType(propertyName, String.class, defaultValue);
	}
	/**
	 * 根据传入的propertyName从form中获取一个Long类型的数据
	 * @param propertyName
	 * @param defaultValue
	 * @return
	 */
	public Long getLong(String propertyName,Long defaultValue){
		return getTargetType(propertyName, Long.class, defaultValue);
	}
	/**
	 * 根据属性名称获取一个Float类型的数据
	 * @param propertyName
	 * @param defaultValue
	 * @return
	 */
	public Float getFloat(String propertyName,Float defaultValue){
		return getTargetType(propertyName, Float.class, defaultValue);
	}
	/**
	 * 根据属性名称获取一个Double类型的数据
	 * @param propertyName
	 * @param defaultValue
	 * @return
	 */
	public Double getDouble(String propertyName,Double defaultValue){
		return getTargetType(propertyName, Double.class, defaultValue);
	}

	/**
	 * 获取一个整数,默认值为 null
	 * @param src
	 * @return
	 */
	public Integer getInt(String src){
		return getInt(src,null);
	}
	/**
	 * 获取一个Long,默认值为 null
	 * @param src
	 * @return
	 */
	public Long getLong(String src){
		return getLong(src,null);
	}
	/**
	 * 获取一个Float,默认值为 null
	 * @param src
	 * @return
	 */
	public Float getFloat(String src){
		return getFloat(src,null);
	}
	/**
	 * 获取一个Double,默认值为 null
	 * @param src
	 * @return
	 */
	public Double getDouble(String src){
		return getDouble(src,null);
	}

	/**
	 * 根据传入属性名称,获取指定类型的数据,能够实现基本数据类型转换
	 * @param <T> 泛型参数
	 * @param propertyName 属性名称
	 * @param targetType 获取值的目标类型
	 * @param defaultValue 默认值
	 * @return 指定类型的数据
	 */
	@SuppressWarnings("unchecked")
	public <T> T getTargetType(String propertyName ,Class<T> targetType,T defaultValue){
		Object rawValue = get(propertyName);
		if(rawValue == null || (rawValue instanceof String && "".equals(rawValue.toString().trim()))){
			return defaultValue;
		}else if(rawValue.getClass() == targetType){
			return (T)rawValue;
		}else{
			return BeanHelper.toTargetType(rawValue.toString(), targetType);
		}
	}

}
